package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.config.PdfParseApiProperties;
import cn.com.haopy.yue.ai.core.utils.HttpUtils;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.wx.web.base.BaseWxController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 登录
 */
@RestController
@RequestMapping("/haopyYueAi/pdfParse")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "pdf解析数据")
public class WxPdfParseController extends BaseWxController {
    @Autowired
    private PdfParseApiProperties pdfParseApiProperties;

    @GetMapping("/data")
    @ApiOperation(value = "数据解析")
    public Object data(@RequestParam String pdfUrl) throws Exception {

        // 调用远程HTTP GET接口，注意这里的请求路径与接口文档一致
        Map<String, Object> params = new HashMap<>();
        params.put("pdf_url", pdfUrl);
        JSONObject jsonObject = HttpUtils.postJSON(pdfParseApiProperties.getUrl() + "parse", null, params);
        // 转换为json
        if (Objects.nonNull(jsonObject)) {
            /*
             --------------项目标准化 注意只返回了匹配上的指标 ----------------------***
             --------------项目标准化 注意只返回了匹配上的指标 ----------------------***
             --------------项目标准化 注意只返回了匹配上的指标 ----------------------***
             */
            try {
                JSONArray array = jsonObject.getJSONArray("data");//标准项目转化
                Map<String, Object> indexParams = new HashMap<>();
                indexParams.put("data", array);

                JSONObject indexResult = HttpUtils.postJSON(pdfParseApiProperties.getUrl() + "parseIndex", null, indexParams);
                // 转换为json
                if (Objects.nonNull(indexResult)) {
                    jsonObject.put("data", indexResult.getJSONObject("data").getJSONArray("data"));
                }

                return ResponseUtil.ok(jsonObject);
            } catch (Exception e) {
                log.error(e.toString());
                return ResponseUtil.fail("指标标准化失败！");
            }

        }
        return ResponseUtil.fail("pdf数据解析错误，请上传正确的体检报告！");

    }

    @PostMapping("/dataIndex")
    @ApiOperation(value = "数据解析标准化")
    public Object dataIndex(@RequestBody JSONArray indexArray) throws Exception {


        //JSONArray indexArray = JSON.parseArray(dataStr);


        try {
            Map<String, Object> indexParams = new HashMap<>();
            indexParams.put("data", indexArray);

            JSONObject indexResult = HttpUtils.postJSON(pdfParseApiProperties.getUrl() + "parseIndex", null, indexParams);
            return ResponseUtil.ok(indexResult.getJSONObject("data").getJSONArray("data"));
        } catch (Exception e) {
            log.error(e.toString());
            return ResponseUtil.fail("指标标准化失败！");
        }


    }


}